import Cookie from "js-cookie"

export default{
   state:{
       isCollapse: false, //控制菜单伸缩
       tabList:[
        {
          path:"/",
          name:"home",
          label:"首页",
          icon:"s-home",
          url:"Home/Home",
        }
       ],
       menu:[]
   },
   mutations:{
     collapseMenu(state){
      state.isCollapse =  !state.isCollapse
     },
     selectMenu(state,item){
          if (item.name !== 'home') {
             if (state.tabList.findIndex(i => i.name === item.name) === -1) {
                state.tabList.push(item)
             }
          } 
     },
     closeTag(state,item){
       const index = state.tabList.findIndex(val => val.name === item.name)
       state.tabList.splice(index,1)
      },
      setMenu(state,val){
         state.menu = val
         Cookie.set('menu',JSON.stringify(val))
      },
      addMenu(state,router){
         if(!Cookie.get('menu')){
             return
         }
         const menu = JSON.parse(Cookie.get('menu'))
         const MenuArray=[]
         menu.forEach(element => {
            if(element.children){
             element.children=  element.children.map(element =>{
                   element.component = ()=> import(`@/view/${element.url}`)
                   return element
                  })
               MenuArray.push(...element.children)
            }else{
               element.component = ()=> import(`@/view/${element.url}`)
               MenuArray.push(element)
            }
         });
         console.log(MenuArray,menu)
         MenuArray.forEach(element =>{
            router.addRoute("mymain",element)
            
         })
      }
   }
}